﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using DistributeTransaction.Models;

namespace DistributeTransaction.DAL
{
    public class ShoppingCartRepository : GenericRepository<ShoppingCart>
    {
        public ShoppingCartRepository(DatabaseContext context)
            : base(context)
        {

        }

        internal IEnumerable<ShoppingCart> GetShoppingCart(Guid userId)
        {
            const string query = @"SELECT C.[ProductID], C.[UserId], C.[Quantity], C.[UnitPrice], C.[Coupon],
		P.ProductName, P.QuantityPerUnit, S.CompanyName
  FROM [dbo].[ShoppingCar] C
  JOIN [dbo].[Product] P  ON C.[ProductID] = P.[ProductID]
  JOIN [dbo].[Suppliers] S ON P.SupplierID = S.SupplierID
WHERE C.UserId = @p0";
            return GetWithRawSql(query, userId).ToList();
        }

        internal void Remove(Guid userId, int productId)
        {
            const string sql = @"dbo.Delete_ShoppingCart";

            SqlParameter pUserId = new SqlParameter();
            pUserId.ParameterName = "UserId";
            pUserId.Direction = ParameterDirection.Input;
            pUserId.DbType = DbType.Guid;
            pUserId.Value = userId;

            SqlParameter pProdId = new SqlParameter();
            pProdId.ParameterName = "ProductId";
            pProdId.Direction = ParameterDirection.Input;
            pProdId.DbType = DbType.Int32;
            pProdId.Value = productId;

            ExecuteStoreProcedure(sql, pUserId, pProdId);
        }

        internal void UpdateShoppingCart(IEnumerable<int> productIds, Guid userId)
        {
            IEnumerable<ShoppingCart> items = GetShoppingCart(userId);
            List<int> list = new List<int>();
            foreach (ShoppingCart item in items)
            {
                if (productIds.Contains(item.ProductID))
                {
                    item.Quantity++;
                    Update(item);
                    list.Add(item.ProductID);
                }
            }

            foreach (int index in productIds)
            {
                if (!list.Contains(index))
                {
                    ShoppingCart item = new ShoppingCart();
                    item.UserId = userId;
                    item.ProductID = index;
                    item.Quantity = 1;
                    Insert(item);
                }
            }
        }

        protected override void Update(ShoppingCart entity)
        {
            const string sql = @"dbo.Update_ShoppingCart";

            SqlParameter pUserId = new SqlParameter();
            pUserId.ParameterName = "UserId";
            pUserId.Direction = ParameterDirection.Input;
            pUserId.DbType = DbType.Guid;
            pUserId.Value = entity.UserId;

            SqlParameter pProdId = new SqlParameter();
            pProdId.ParameterName = "ProductId";
            pProdId.Direction = ParameterDirection.Input;
            pProdId.DbType = DbType.Int32;
            pProdId.Value = entity.ProductID;

            SqlParameter pQuantity = new SqlParameter();
            pQuantity.ParameterName = "Quantity";
            pQuantity.Direction = ParameterDirection.Input;
            pQuantity.DbType = DbType.Int32;
            pQuantity.Value = entity.Quantity;

            ExecuteStoreProcedure(sql, pUserId, pProdId, pQuantity);
        }

        protected override void Insert(ShoppingCart entity)
        {
            const string sql = @"dbo.Insert_ShoppingCart";

            SqlParameter pUserId = new SqlParameter();
            pUserId.ParameterName = "UserId";
            pUserId.Direction = ParameterDirection.Input;
            pUserId.DbType = DbType.Guid;
            pUserId.Value = entity.UserId;

            SqlParameter pProdId = new SqlParameter();
            pProdId.ParameterName = "ProductId";
            pProdId.Direction = ParameterDirection.Input;
            pProdId.DbType = DbType.Int32;
            pProdId.Value = entity.ProductID;

            ExecuteStoreProcedure(sql, pUserId, pProdId);
        }

       
        internal void EmptyCart(Guid userId)
        {
            Remove(userId, -1);
        }

        internal void Save(ShoppingCart sc)
        {
            Update(sc);
        }
    }
}